#include <common/common.h>

namespace common
{

std::vector<std::string> split_param_name(const std::string& str) 
{  
    std::vector<std::string> result;
    std::stringstream ss(str);
    std::string element;
    while (std::getline(ss, element, '/')) {  
        result.push_back(element);  
    }  
    return result;  
}

bool is_element_existed(std::string element, std::vector<std::string> vec)
{
	if (vec.size() == 0)
		return false;

	auto it = std::find(vec.begin(), vec.end(), element);  
  
    if (it != vec.end())
    	return true;
    else
    	return false;
}

std::vector<std::string> get_single_layer_param_names(std::string name_space_to_find)
{
    std::vector<std::string> param_names;
	std::vector<std::string> result;
    std::vector<std::string> split_name_space = split_param_name(name_space_to_find);

    if (ros::param::getParamNames(param_names)) {
  		for (const auto param : param_names) {
  			// RCLCPP_INFO(rclcpp::get_logger("Common"), "handle param: %s", param.c_str());
  			if (param.length() >= name_space_to_find.length() && param.compare(0, name_space_to_find.length(), name_space_to_find) == 0) {
	    		std::vector<std::string> split_param = split_param_name(param);
	    		// RCLCPP_INFO(rclcpp::get_logger("Common"), "get param: %s  split size: %d %d", param.c_str(), split_param.size(), split_name_space.size());
	    		if (split_param.size() > split_name_space.size() && !is_element_existed(split_param[split_name_space.size()], result)) {
	    			result.push_back(split_param[split_name_space.size()]);
	    		}
    		}	
  		}
	}
	// else {
	// 	RCLCPP_ERROR(rclcpp::get_logger("Common"), "[Common] cannot get param names");
	// }

	return result;
}

}